package com.jdk.learn.thread;

import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * Created by baiguantao on 2018/1/24.
 */
public class TestFuture {

    public static void main(String[] args) {
        SynRecompileDemo sd=new SynRecompileDemo();
        new Thread(sd).start();
        new Thread(sd).start();
        new Thread(sd).start();

    }
    public static void main2(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(4);
        long start = System.currentTimeMillis();
        List<Future<Object>> list = new LinkedList<>();
        for (int i = 0; i < 10; i++) {
            list.add(executorService.submit(() -> {
                Thread.sleep(10);
                return Math.random();
            }));
        }
        System.out.println(list.size());
//         for (Future<Object> f:list) {
//            while (!f.isDone());
//                try {
//                    System.out.println(f.get().toString());
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                } catch (ExecutionException e) {
//                    e.printStackTrace();
//                }
//            }
        for (Future<Object> f:list) {
                try {
                    System.out.println(f.get().toString());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
        }

        long end=System.currentTimeMillis();

        System.out.println(end-start);

        executorService.shutdown();
        executorService=null;
    }
}
